$(function() {
	//--------------获取url内容
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null)
		return unescape(r[2]);
	return null;
}
//--------------封装时间格式
function dateTime(time) { //获取时间相关数据
	var now = new Date();
	var myDate = new Date(time);
	//获取时间戳
	var timestamp = new Date(time).getTime();
	//获取当前年
	var year = myDate.getFullYear();
	//获取当前月
	var month = myDate.getMonth() + 1;
	//获取当前日
	var date = myDate.getDate();
	var h = myDate.getHours(); //获取当前小时数(0-23)
	var m = myDate.getMinutes(); //获取当前分钟数(0-59)
	var s = myDate.getSeconds();
	//输出时间相关数据
	var time_local = year + '-' + month + "-" + date;
	return time_local;
}
	
	
	var contract = decodeURI(getUrlParam('contract'));
	var beginSettle = decodeURI(getUrlParam('beginSettle'));
	var endSettle = decodeURI(getUrlParam('endSettle'));
	var topName = sessionStorage.getItem('topName');

	function init(contract, beginSettle, endSettle, topName) {
		$('header span').html(topName);
		var fromYear = beginSettle.substring(0, 4);
		var fromMonth = beginSettle.substring(4, 6);
		var toYear = endSettle.substring(0, 4);
		var toMonth = endSettle.substring(4, 6);

		$('#fromYear').html(fromYear);
		$('#fromMonth').html(fromMonth);
		$('#toYear').html(toYear);
		$('#toMonth').html(toMonth);
		var LoadParam = {
			contract: contract,
			beginSettle: beginSettle,
			endSettle: endSettle
		};

		$.ajax({
			url: "querybycontract.thor",
			cache: false,
			data: {
				LoadParam: JSON.stringify(LoadParam)
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				var receiptRate = null;
				if(data.payTotal == 0) {
					receiptRate = 0.00;
				} else {
					receiptRate = data.payedTotal / data.payTotal;
				}

				$('#jiaokuanlvnum').html(receiptRate.toPercent());
				$('#yingjiaonum').html(data.payTotal);
				$('#yijiaonum').html(data.payedTotal);
				$('#weijiaonum').html(data.unpayedTotal);
				$('#phonenum').html(JSON.parse(localStorage.getItem("loginInfo")).telephone);

				var subjects = data.subjects;
				for(var i = 0; i < subjects.length; i++) {
					$('#scroller ul.display').append('<li>' +
						'<dl>' +
						'<dt><img src="../pages/bill/img/money_add.png"/></dt>' +
						'<dd>' +
						'<p>' + subjects[i].subject.code + ' &nbsp;<span>' + subjects[i].subject.name + '</span></p>' +
						'<p class="limitTime">' + dateTime(subjects[i].beginDate) + '至' + dateTime(subjects[i].endDate) + '</p>' +
						'<div class="yingjiao">' +
						'<p>[应缴]</p>' +
						'<p>' + subjects[i].payTotal + '元</p>' +
						'</div>' +
						'<div class="line"></div>' +
						'</dd>' +
						'</dl>' +
						'<div class="weijiao">' +
						'<p class="green">[已缴] <span class="yjnum green">' + subjects[i].payedTotal + '</span>元</p>' +
						'<p class="red">[未缴] <span class="wjnum red">' + subjects[i].unpayedTotal + '</span>元</p>' +
						'</div>' +
						'<div class="rowLine"></div>' +
						'</li>');

				}

			},
			error: function(data) {
				var data = JSON.stringify(data)
				layer.msg("查询失败")
			}
		});
	}
	init(contract, beginSettle, endSettle, topName);

	//--------------是否显示结清账户
	$('#settleBill').change(function() {
		var count = 0;
		if(!$('#settleBill').prop('checked')) {
			$('#scroller ul.display li').each(function() {
				var self = this;
				if(parseInt($(self).find('span.wjnum').text()) <= 0) {
					count += Number($(self).find('span.wjnum').text());
					$(self).hide();
				}
			})
			$('#yijiaonum').html(Number($('#yijiaonum').text()) - count);
		} else {
			$('#scroller ul.display li').each(function() {
				var self = this;
				if(parseInt($(self).find('span.wjnum').text()) <= 0) {
					count += Number($(self).find('span.wjnum').text());
					$(self).show();
				}
			})
			$('#yijiaonum').html(Number($('#yijiaonum').text()) + count);
		}
	})
})